import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';

import { AuthService } from './auth.service';
import { JwtHelperService } from '@auth0/angular-jwt';

@Injectable()
export class AuthGuard implements CanActivate {
    constructor(private authService: AuthService, private router: Router, private jwt: JwtHelperService) { }

    canActivate() {
        if (this.jwt.isTokenExpired()) {
            this.router.navigate(['login']);
            return false;
        } else {
            this.authService.getUser().then((res) => {
                // console.log(res);
                return true;
            });
            return true;
        }

    }
}
